package sjtu.ist.warehouse4ws.action;

import java.util.ArrayList;
import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.apache.struts2.json.annotations.JSON;

import sjtu.ist.warehouse4ws.model.CustDepartment;
import sjtu.ist.warehouse4ws.model.CustModelgroup;
import sjtu.ist.warehouse4ws.model.Department;
import sjtu.ist.warehouse4ws.model.Modelgroup;
import sjtu.ist.warehouse4ws.service.DepartmentService;
import sjtu.ist.warehouse4ws.service.ModelgroupService;
import sjtu.ist.warehouse4ws.service.cust.CustDepartmentService;
import sjtu.ist.warehouse4ws.service.cust.CustModelgroupService;

import com.opensymphony.xwork2.ActionSupport;

public class ShowDepartmentAction extends ActionSupport{
	private List<Department> departmentList=new ArrayList<Department> ();
	private DepartmentService departmentService;
	private ModelgroupService modelgroupService;
	private CustDepartmentService custDepartmentService;
	private CustModelgroupService custModelgroupService;
	private String project;
	private List<Department> companyList = new ArrayList<Department>();
	private String uid;
	private String fileId;
	private String gid;
	private String vid;
	
	
	public String getProject() {
		return project;
	}

	public void setProject(String project) {
		this.project = project;
	}
	
	public String getFileId() {
		return fileId;
	}

	public void setFileId(String fileId) {
		this.fileId = fileId;
	}

	public String getUid() {
		return uid;
	}

	public void setUid(String uid) {
		this.uid = uid;
	}
	
	public void setDepartmentService(DepartmentService departmentService) {
		this.departmentService = departmentService;
	}
	
	public void setModelgroupService(ModelgroupService modelgroupService) {
		this.modelgroupService = modelgroupService;
	}

	public void setCustDepartmentService(CustDepartmentService custDepartmentService) {
		this.custDepartmentService = custDepartmentService;
	}

	public void setCustModelgroupService(CustModelgroupService custModelgroupService) {
		this.custModelgroupService = custModelgroupService;
	}

	public String getGid() {
		return gid;
	}

	public void setGid(String gid) {
		this.gid = gid;
	}

	public String getVid() {
		return vid;
	}

	public void setVid(String vid) {
		this.vid = vid;
	}

	public String showDepartment(){
		
		Subject cuser=SecurityUtils.getSubject();
		if(cuser.hasRole("superadmin")){
			List<Modelgroup> temp=modelgroupService.getByName(project);
			if (temp.size()!=0){
				this.departmentList=departmentService.getDepartmentByGroupId(temp.get(0).getId());
			}
		}else{
			List<CustModelgroup> list=custModelgroupService.getByName(project);
			if (list.size()!=0){
				List<CustDepartment> tlist=custDepartmentService.getDepartmentByGroupId(list.get(0).getId());
				for(CustDepartment rec : tlist){
					this.departmentList.add(rec);
				}				
			}
		}
		
		

		return SUCCESS;
	}
	public String showDepartmentGraph(){
		departmentList=departmentService.getDepartmentByFileId(fileId);
		//departmentList=departmentService.getDepartmentByGroupAndVersion(gid,vid);
		System.out.print(departmentList.size());
		return SUCCESS;
	}
	
	@JSON(name="totalDepartment")	
	public int getTotalDepartment() {
		return this.departmentList.size();
	}
    
	@JSON(name="Department")	
	public List<Department> getDepartmentList() {
		return this.departmentList;
	}

	//获得组织中Company列表
	public String showCompanyList(){		
		//this.companyList = departmentService.getCompanyList();
		this.companyList = departmentService.getCompanyListByGroupAndVersion(gid,vid);
		return SUCCESS;
	}
	
	
	@JSON(name = "totalCompany")
	public int getTotalCompany() {
		return this.companyList.size();
	}
	
	@JSON(name = "Company")
	public List<Department> getCompanyList() {
		return this.companyList;
	}
}
